﻿using AutoMapper;
using Compass.PlanService.Domain;
using Compass.PlanService.Domain.Entities;
using Compass.PlanService.Infrastructure;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Plans;

namespace Compass.Wasm.Server.Services.Plans;

public interface IPalletService : IBaseService<PalletDto>
{
    Task<ApiResponse<PalletDto>> UpdateStatusesAsync(Guid id, PalletDto dto);
    Task<ApiResponse<IEnumerable<PalletDto>>> GetAllByMainPlanAsync(Guid mainPlanId);
}
public class PalletService : IPalletService
{
    #region ctor
    private readonly PlanDbContext _dbContext;
    private readonly IPlanRepository _repository;
    private readonly IMapper _mapper;
    public PalletService(PlanDbContext dbContext, IPlanRepository repository, IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    }
    #endregion
    
    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<PalletDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetPalletsAsync();
            var orderModels = models.OrderByDescending(x => x.PalletNumber);
            var dtos = await _mapper.ProjectTo<PalletDto>(orderModels).ToListAsync();
            return new ApiResponse<IEnumerable<PalletDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<PalletDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<PalletDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetPalletByIdAsync(id);
            if (model == null) return new ApiResponse<PalletDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<PalletDto>(model);
            return new ApiResponse<PalletDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<PalletDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<PalletDto>> AddAsync(PalletDto dto)
    {
        try
        {
            //在此继续避免重复添托盘
            var existPallet = await _repository.GetPalletByPalletNumber(dto.MainPlanId.Value,dto.PalletNumber.Value);
            if (existPallet != null)
            {
                existPallet.Update(dto);
                dto.Id = existPallet.Id;
                return new ApiResponse<PalletDto> { Status = true, Result = dto };
            }
            
            var model = new Pallet(Guid.NewGuid(), dto.MainPlanId.Value, dto.PalletNumber.Value, dto.MtlNumber, dto.Type, dto.Length, dto.Width, dto.Height, dto.PalletLength, dto.PalletWidth, dto.PalletHeight, dto.GrossWeight, dto.NetWeight, dto.ShowItem, dto.Remark);

            await _dbContext.Pallets.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<PalletDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<PalletDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<PalletDto>> UpdateAsync(Guid id, PalletDto dto)
    {
        try
        {
            var model = await _repository.GetPalletByIdAsync(id);
            if (model == null) return new ApiResponse<PalletDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);

            return new ApiResponse<PalletDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<PalletDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<PalletDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetPalletByIdAsync(id);
            if (model == null) return new ApiResponse<PalletDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<PalletDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<PalletDto> { Status = false, Message = e.Message };
        }
    }

    #endregion

    #region 扩展查询
    public async Task<ApiResponse<PalletDto>> UpdateStatusesAsync(Guid id, PalletDto dto)
    {
        try
        {
            var model = await _repository.GetPalletByIdAsync(id);
            if (model == null) return new ApiResponse<PalletDto> { Status = false, Message = "更新数据失败" };
            model.UpdateStatuses(dto);
            //todo:细化逻辑只记录最早入库和最早发货的时间？

            var mainPlan = await _repository.GetMainPlanByIdAsync(dto.MainPlanId.Value);
            var mainPlanDto = _mapper.Map<MainPlanDto>(mainPlan);
            mainPlanDto.WarehousingTime=dto.WarehousingTime;
            mainPlanDto.ShippingTime=dto.ShippingTime;
            mainPlan.UpdateStatuses(mainPlanDto);
            return new ApiResponse<PalletDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<PalletDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<IEnumerable<PalletDto>>> GetAllByMainPlanAsync(Guid mainPlanId)
    {
        try
        {
            var models = await _repository.GetPalletsByMainPlanIdAsync(mainPlanId);
            var dtos = _mapper.ProjectTo<PalletDto>(models).OrderBy(x => x.PalletNumber).ToList();
            return new ApiResponse<IEnumerable<PalletDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<PalletDto>> { Status = false, Message = e.Message };
        }
    }

    
    #endregion

}